package com.guli.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.response.PageResult;
import com.guli.common.response.Result;
import com.guli.model.product.Brand;
import com.guli.model.product.BrandSearchDTO;
import com.guli.product.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class BrandService {
    @Autowired
    BrandMapper brandMapper;


    //分页
    public PageResult<Brand> findPage(Integer page, Integer size, Map searchMap) {
        //分页
        Page<Brand> page1 = new Page<>(page,size);
        //模糊
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        String keyword = (String) searchMap.get("keyword");
        //条件 名字模糊
        if(StringUtils.isNoneEmpty(keyword)){
            queryWrapper.lambda().like(Brand::getName, keyword)
                    .or().like(Brand::getDescript, keyword)
                    .or().eq(Brand::getFirstLetter, keyword);
        }
        Page<Brand> brandPage = brandMapper.selectPage(page1, queryWrapper);
        return new PageResult<>(brandPage.getTotal(), brandPage.getRecords());
    }


    //id查询
    public Brand selectById(Long id) {
        Brand brand = brandMapper.selectById(id);
        return brand;

    }


    //id 删除
    public Result deleteById(Long id) {
        brandMapper.deleteById(id);
        return new Result(true, "1", "删除成功");
    }

    //添加修改
    public Result save(Brand brand) {
        //有id 修改 无id 添加
        if(brand.getBrandId()!=null){
            brandMapper.updateById(brand);
            return new Result(true, "1", "修改成功");
        }else{
            brandMapper.insert(brand);
            return new Result(true, "1", "添加成功");
        }
    }
}
